﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

namespace TPG.SharePoint.Workflow
{
    public class HashtableTaskDataProvider : TaskDataProvider
    {
        Hashtable _properties;

        //public HashtableTaskDataProvider()
        //{
        //    _properties = new Hashtable();
        //}

        internal HashtableTaskDataProvider(SPListItem listItem)
            : base(listItem)
        {
            _properties = SPWorkflowTask.GetExtendedPropertiesAsHashtable(listItem);
        }

        internal override void CommitTask()
        {
            SPWorkflowTask.AlterTask(this.ListItem, _properties, true);
        }

        public override PropertyDescriptor[] BuildDescriptors()
        {
            PropertyDescriptor[] propertyDescriptors = new PropertyDescriptor[_properties.Keys.Count];
            int index = 0;
            foreach (string key in _properties.Keys)
                propertyDescriptors[index++] = new CustomPropertyDescriptor(key, this);
            return propertyDescriptors;
        }

        public override void SetValue(string name, object value)
        {
            _properties[name] = value;
        }

        public override object GetValue(string name)
        {
            return _properties[name];
        }

        internal class CustomPropertyDescriptor : PropertyDescriptor
        {
            HashtableTaskDataProvider _provider;

            public CustomPropertyDescriptor(string name, HashtableTaskDataProvider provider)
                : base(name, null)
            {
                _provider = provider;
            }

            public override bool CanResetValue(object component)
            {
                return false;
            }

            public override Type ComponentType
            {
                get { return typeof(TaskData); }
            }

            public override object GetValue(object component)
            {
                TaskData data = component as TaskData;
                if (data == null)
                    throw new ArgumentException("Expected an object of type TaskData", "component");
                return _provider.GetValue(this.Name);
            }

            public override bool IsReadOnly
            {
                get { return true; }
            }

            public override Type PropertyType
            {
                get { return typeof(object); }
            }

            public override void ResetValue(object component)
            {
                throw new NotImplementedException();
            }

            public override void SetValue(object component, object value)
            {
                throw new NotImplementedException();
            }

            public override bool ShouldSerializeValue(object component)
            {
                return false;
            }
        }
    }
}
